package cjq.demo.thread.ch5_2;

import java.util.List;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.TimeUnit;

/**
 * Created by Administrator on 2016/5/1.
 */
public class Main {
    public static void main(String[] args) {
        ProductListGenerator generator = new ProductListGenerator();
        List<Product> products = generator.generate(100);
        Task task = new Task(products,0,products.size(),0.20);

        ForkJoinPool pool = new ForkJoinPool();
        pool.execute(task);

        do{
            System.out.println("Main: Thread Count : "+pool.getActiveThreadCount());
            System.out.println("Main: Thread Steal : "+pool.getStealCount());
            System.out.println("Main: Parallelism : "+pool.getParallelism());
            try{
                TimeUnit.MILLISECONDS.sleep(5);
            }catch(InterruptedException e){
                e.printStackTrace();
            }
        }while(!task.isDone());

        pool.shutdown();
        if(task.isCompletedNormally()){
            System.out.println("Main: The process has completed normally");
        }

        for (int i = 0; i < products.size(); i++) {
            Product product = products.get(i);
            if(product.getPrice()!=12){
                System.out.println("Product "+product.getName()+" : "+product.getPrice());
            }
        }

        System.out.println("main :End of the program.");
    }
}
